package com.hoshiibuy.sys.permission.controller;

import java.time.LocalDateTime;
import java.util.List;

import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiibuy.common.rpc.Response;
import com.hoshiibuy.sys.permission.entity.SysUserRole;
import com.hoshiibuy.sys.permission.entity.SysUsers;
import com.hoshiibuy.sys.permission.entity.param.SysUsersInsertParam;
import com.hoshiibuy.sys.permission.entity.param.SysUsersQueryParam;
import com.hoshiibuy.sys.permission.entity.param.SysUsersUpdateParam;
import com.hoshiibuy.sys.permission.entity.vo.SysUsersDetailVO;
import com.hoshiibuy.sys.permission.entity.vo.SysUsersVO;
import com.hoshiibuy.sys.permission.service.ISysUserRoleService;
import com.hoshiibuy.sys.permission.service.impl.SysUsersServiceImpl;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 系统用户控制器
 * 
 * @author Mr.wu
 *
 */
@Api(tags = "系统用户接口")
@RestController
@RequestMapping("/user")
public class SysUserController {

	@Autowired
	private SysUsersServiceImpl sysUsersService;

	@Autowired
	private ISysUserRoleService sysUserRoleService;

	@ApiOperation("新增系统用户")
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Response addSysUser(@Valid SysUsersInsertParam param, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return Response.failed("参数有误:" + bindingResult.getFieldError().toString());
		}
		SysUsers user = new SysUsers();
		BeanUtils.copyProperties(param, user);
		// TODO 获取创建人
		user.setCreatedby("admin");
		// TODO 只接受指定参数
		// 接收角色数组，并新增对应关联

		return sysUsersService.addSysUser(user);
	}

	@ApiOperation("修改系统用户")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public Response updateSysUser(@Valid SysUsersUpdateParam param, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return Response.failed("参数有误:" + bindingResult.getFieldError().toString());
		}
		SysUsers entity = new SysUsers();
		BeanUtils.copyProperties(param, entity);
		// TODO 参数过滤：只接受指定参数
		entity.setModifiedtime(LocalDateTime.now());
		return sysUsersService.updateSysUser(entity);
	}

	@ApiOperation("删除系统用户")
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public Response deleteSysUser(@RequestParam String id) {
		if (StringUtils.isBlank(id)) {
			return Response.failed("id不能为空");
		}
		return sysUsersService.deleteSysUser(Long.valueOf(id));
	}

	@ApiOperation("获取系统用户详情")
	@RequestMapping(value = "/getDetail", method = RequestMethod.GET)
	public Response<SysUsersDetailVO> getSysUserDetail(@RequestParam String id) {
		if (StringUtils.isBlank(id)) {
			return Response.failed("id不能为空");
		}
		return sysUsersService.getSysUserDetail(Long.valueOf(id));
	}

	@ApiOperation("获取系统用户列表")
	@RequestMapping(value = "getList", method = RequestMethod.GET)
	public Response<List<SysUsersVO>> getSysUserList(@Valid SysUsersQueryParam param, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return Response.failed("参数有误:" + bindingResult.getFieldError().toString());
		}
		return sysUsersService.getSysUserList(param.getQueryWrapper());
	}

	@ApiOperation("获取系统用户所拥有的角色")
	@RequestMapping(value = "getRoleList", method = RequestMethod.GET)
	public Response<List<SysUserRole>> getSysUserRoleList(@RequestParam String id) {
		if (StringUtils.isBlank(id)) {
			return Response.failed("id不能为空");
		}
		SysUserRole param = new SysUserRole();
		param.setUserId(Long.valueOf(id));
		QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<SysUserRole>(param);
		return sysUserRoleService.getSysUserRoleList(queryWrapper);

	}

	@ApiOperation(value = "获取用户所拥有的权限", notes = "返回权限值列表")
	@RequestMapping(value = "getUserPermission", method = RequestMethod.GET)
	public Response<List<String>> getSysUserPermissionList(@RequestParam String id) {
		return sysUsersService.getSysUserPermissionList(Long.valueOf(id));
	}

	@ApiOperation("分页查询系统用户")
	@RequestMapping(value = "/getPage", method = RequestMethod.GET)
	public Response<IPage<SysUsersVO>> getSysUserPage(@Valid SysUsersQueryParam param, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return Response.failed("参数有误:" + bindingResult.getFieldError().toString());
		}
		QueryWrapper<SysUsers> queryWrapper = param.getQueryWrapper();

		IPage<SysUsers> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));

		return sysUsersService.getPage(page, queryWrapper);
	}

}
